package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.NeItem;

import org.w3c.dom.Text;

/**
 * Created by apple on 16/4/17.
 */
public class ModifyNeItemDialog extends Dialog implements View.OnClickListener {

    private View mDialogView;
    private TextView neIdView;
    private EditText nameInput, ipInput, macInput;
    private View confirmBtn,closeBtn;

    private OnNeItemModifiedListener onNeItemModifiedListener;

    private NeItem modifyedNeItem;

    public ModifyNeItemDialog(Context context) {
        super(context);
        init(context);
    }

    public ModifyNeItemDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected ModifyNeItemDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
    }


    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_modify_neitem_layout, null);
        nameInput = (EditText) mDialogView.findViewById(R.id.device_name_input);
        ipInput = (EditText) mDialogView.findViewById(R.id.device_ip_input);
        macInput = (EditText) mDialogView.findViewById(R.id.device_mac_input);
        neIdView= (TextView) mDialogView.findViewById(R.id.ne_id);
        closeBtn=mDialogView.findViewById(R.id.close_btn);
        confirmBtn = mDialogView.findViewById(R.id.modify_device_confirm_btn);
        confirmBtn.setOnClickListener(this);
        closeBtn.setOnClickListener(this);
        setContentView(mDialogView);
    }


    public ModifyNeItemDialog setNeItem(NeItem neItem) {
        if (null!=neItem)
        {
            this.modifyedNeItem = new NeItem(neItem);
            neIdView.setText(neItem.getId());
            nameInput.setText(TextUtils.isEmpty(modifyedNeItem.getNeName()) ? "" : modifyedNeItem.getNeName());
            ipInput.setText(TextUtils.isEmpty(modifyedNeItem.getNeIp()) ? "" : modifyedNeItem.getNeIp());
            macInput.setText(TextUtils.isEmpty(modifyedNeItem.getMac()) ? "" : modifyedNeItem.getMac());
        }
        return this;
    }


    public ModifyNeItemDialog setOnNeItemModifiedListener(OnNeItemModifiedListener onNeItemModifiedListener) {
        this.onNeItemModifiedListener = onNeItemModifiedListener;

        return this;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.modify_device_confirm_btn:
                if (null != onNeItemModifiedListener) {


                    String newName = nameInput.getText().toString().trim();
                    if (TextUtils.isEmpty(newName))
                    {
                        Toast.makeText(getContext(),"网元名称不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    String newIp = ipInput.getText().toString().trim();
                    if (TextUtils.isEmpty(newIp))
                    {
                        Toast.makeText(getContext(),"网元IP地址不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    String newMac = macInput.getText().toString().trim();
                    if (TextUtils.isEmpty(newMac))
                    {
                        Toast.makeText(getContext(),"网元Mac地址不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }

                    modifyedNeItem.setNeName(newName);
                    modifyedNeItem.setNeIp(newIp);
                    modifyedNeItem.setMac(newMac);
                    onNeItemModifiedListener.neItemModifiedListener(modifyedNeItem);
                    dismiss();
                }else
                {
                    dismiss();
                }
                break;
            case R.id.close_btn:
                dismiss();
                break;
        }

    }

    public interface OnNeItemModifiedListener {
        public void neItemModifiedListener(NeItem item);
    }
}
